/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import Enum.LoaiNguoiDungEnum;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.NguoiDung;
import pojo.SanPhamCuaHang;
import util.NewHibernateUtil;


/**
 *
 * @author 0
 */
public class SanPhamCuaHangDAO extends BaseDAO{
    
//    public void TextTenCuaHang(String str){
//        String hql ="select new Pojo.CuaHang(ch.ten_cua_hang) from CuaHang ch";
//    }
//     public static List<SanPhamCuaHang> LayDanhSachSanPhamCuaHang(Class<?> clazz,NguoiDung nguoiDung) throws Exception{
//        Session session=NewHibernateUtil.getSessionFactory().openSession();
//        String string=String.format("from SanPhamCuaHang spch, CuaHang ch where ((spch.isDelete=false or spch.isDelete=null) and ch.isDelete=false or ch.isDelete=null)",clazz.getName());
//        if(nguoiDung.getLoaiNguoiDung().getId()!=LoaiNguoiDungEnum.Admin.getCode() && existCuaHang(clazz.getDeclaredFields())){
//            string+=String.format(" and spch.cuaHang.id=%d",nguoiDung.getCuaHang().getId());
//        }
//        List<SanPhamCuaHang> list=session.createQuery(string).list();
//        session.close();
//        return list;
//    }
    public static void ThemSanPham_CuaHang(SanPhamCuaHang sanPham_cuaHang) throws Exception{
        Them(sanPham_cuaHang);
    }
     
    public static void XoaSanPham_CuaHang(Object[] sanPham_CuaHanglist) throws Exception {
         Xoa(sanPham_CuaHanglist);
    }
    
    public static void SuaSanPham_CuaHang(SanPhamCuaHang sanPham_cuaHang) throws Exception{
        Sua(sanPham_cuaHang);
    }
   
     public static List<SanPhamCuaHang> TimKiemSanPhamCuaHang(Integer maCuaHang,String tenCuaHang,Integer maSanPham,String tenSanPham,boolean forDialogSearch) throws Exception{
        Session session=NewHibernateUtil.getSessionFactory().openSession();
        String string="from SanPhamCuaHang spch where (spch.isDelete=false or spch.isDelete=null)";
//        if (nguoiDung.getLoaiNguoiDung().getId() != LoaiNguoiDungEnum.Admin.getCode() || forDialogSearch) {
//            string += String.format(" and spch.cuaHang.id=%d", nguoiDung.getCuaHang().getId());
//        }
        if(maCuaHang!=null){
            string+=String.format(" and spch.cuaHang=%d", maCuaHang);
        }
        if(!tenCuaHang.isEmpty()){
            string+=" and lower(spch.cuaHang.tenCuaHang) like :tenCuaHang";
        }
        if(maSanPham!=null){
             string+=String.format(" and spch.sanPham=%d", maSanPham);
        }
         if(!tenSanPham.isEmpty()){
            string+=" and lower(spch.sanPham.tenSanPham) like :tenSanPham";
        }
        Query query=session.createQuery(string);
        if(!tenCuaHang.isEmpty()){
            query.setString("tenCuaHang", "%"+tenCuaHang.toLowerCase()+"%");
        }
        if(!tenSanPham.isEmpty()){
            query.setString("tenSanPham", "%"+tenSanPham.toLowerCase()+"%");
        }
        List<SanPhamCuaHang>sanPhamcuaHangs=query.list();
        session.close();
        return sanPhamcuaHangs;
    }
    
}
